/**
 * Created by zac on 2017/11/25.
 */
let user = require('./User.js');

let http = require('http');
let url = require('url');
//转换url为字符串测试的时候用
let util = require('util');
//读取文件
let fs = require('fs');

//创建一个服务器
let server = http.createServer((req,res) => {
  // res.statusCode = 200;
  // res.setHeader("Content-Type","text/plain;charset=utf-8");
  // console.log("url:"+req.url); //url:/index.html?user=123
  console.log("parse:"+url.parse(req.url)); //parse:[object Object]
  /*
  * inspect:Url {
   protocol: null,
   slashes: null,
   auth: null,
   host: null,
   port: null,
   hostname: null,
   hash: null,
   search: '?user=123',
   query: 'user=123',
   pathname: '/index.html',
   path: '/index.html?user=123',
   href: '/index.html?user=123' }
   */
  console.log("inspect:"+util.inspect(url.parse(req.url))); //util.inspect():对象转换为字符串

  //读取静态html
  var pathname = url.parse(req.url).pathname;
  fs.readFile(pathname.substr(1),(err,data) => {
    if(err){
      res.writeHead(404,{
        'Content-Type':'text/html'
      });
    }else{
      res.writeHead(200,{
        'Content-Type':'text/html'
      });
      res.write(data.toString());
    }
    res.end();
  })
  // res.end(util.inspect(url.parse(req.url)));
});

server.listen(3000,'127.0.0.1',() => {
    console.log("服务器已运行");
});

